@using MyBlogServer.Services
@inject PostService PostService
@inject IJSRuntime JSRuntime

<div class="tag-cloud">
    <h3>标签云</h3>
    @if (tags.Any())
    {
        <div class="tags">
            @foreach (var tag in tags)
            {
                <a href="/tag/@tag" class="tag">@tag</a>
            }
        </div>
    }
    else
    {
        <p>暂无标签</p>
    }
</div>

@code {
    private IEnumerable<string> tags = new List<string>();

    protected override void OnInitialized()
    {
        tags = PostService.GetAllTags();
    }
    
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            // 调用JavaScript函数为标签分配随机颜色
            await JSRuntime.InvokeVoidAsync("assignRandomTagColors");
        }
    }
}